<template>
	<view class="container">
		<boss-view v-if="userType==1" :userInfo="userInfo" :completePercent="completePercent" :isShowBoss="isShowBoss">
		</boss-view>
		<staff-view v-else :userInfo="userInfo" :completePercent="completePercent" :isShowStaff="isShowStaff">
		</staff-view>
	</view>
</template>
<script>
	import staffView from './staff-my.vue'
	import bossView from './boss-my.vue'
	import {
		he_evaluate_lv,
	} from '@/libs/unit.js'
	export default {
		components: {
			staffView,
			bossView
		},
		data() {
			return {
				isShowBoss: 0,
				isShowStaff: 0,
				userInfo: {
					user: {},
					params: {}
				}, //当前用户信息
				userType: 0, //用户类型0 雇员，1雇主
				completePercent: 14, //资料完成度
			}
		},
		onReady() {
			//#ifdef MP-ALIPAY
			if (my.canIUse('hideBackHome')) {
				this.navbarInit()
			}
			//#endif
		},
		methods: {
			/* 初始化支付宝小程序navbar */
			navbarInit() {
				my.hideBackHome()
				my.hideAddToDesktopMenu()
			},
			// 获取当前用户信息
			async getUserInfo() {
				let res = await this.$fetch(this.$api.get_current_user, {}, 'GET')
				console.log(res,'数据返回');
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				// let haoping = uni.getStorageSync('heEvaluateLv')[uni.getStorageSync('heEvaluateLv').indexOf(res.data.user.userId)+1]
				// console.log(haoping)
				// if(!haoping && haoping!==0){
				// }
				// 整理数据,res.data.user为空，则改为""
				Object.getOwnPropertyNames(res.data.user).forEach(function(key) {
					res.data.user[key] = res.data.user[key] == null ? '' : res.data.user[key]
				})
				// <!-- 新增修改 49-->
				res.data.user.pic = res.data.user.avatarto ? uni.getStorageSync('imgPath') + res.data.user.avatarto :
					''
				let haoping = await he_evaluate_lv(res.data.user.userId)
				res.data.user.sex = res.data.user.sex.toString() //将性别数字变成字符串
				res.data.haoping = haoping
				uni.setStorageSync('userInfo', res.data)
				
				uni.setStorageSync('userType', res.data.userType)
				uni.setStorageSync("level", res.data.user.level) //用户等级
				setTimeout(() => {
					this.userInfo = res.data
				}, 100)
				this.userType = uni.getStorageSync('userType')
				this.computePercent(this.userInfo)
				this.setTabr()
				this.getServiceTelephone()
			},
			// 个人资料完成度
			computePercent(userInfo) {
				let count = 0
				for (let i = 0; i < 7; i++) {
					let arr = ['avatar', 'userName', 'age', 'sex', 'stature', 'edu', 'gxSign']
					if (userInfo.user[arr[i]]) {
						// console.log('res[]', i,arr[i],userInfo.user[arr[i]], )
						count = count + 1
					}
					this.completePercent = Math.round(count / 7 * 100)
				}
			},
			// 切换底部导航栏
			setTabr() {
				if (uni.getStorageSync('userType') == 1) {
					uni.hideTabBar()
				} else {
					setTimeout(() => {
						uni.setTabBarItem({
							index: 0,
							text: '零工',
							"iconPath": "static/tab-bar/1.png",
							"selectedIconPath": "static/tab-bar/2.png",
						})
						uni.setTabBarItem({
							index: 1,
							text: '接单',
							"iconPath": "static/tab-bar/3.png",
							"selectedIconPath": "static/tab-bar/4.png",
						})
					}, 1000)
				}
			},
			async getServiceTelephone() {
				let res = await this.$fetch(this.$api.property_list_by_type, {
					type: 1
				}, 'POST')
				// 实名认证诚信值配置
				uni.setStorageSync('real_auth_integrity', res.data.real_auth_integrity)
				// 客服电话
				uni.setStorageSync('serviceTelephone', res.data.telephone)
					++this.isShowStaff
					++this.isShowBoss
			},
		},
		onShow() {
			uni.hideTabBar()
			if (!uni.getStorageSync('token')) {
				// uni.reLaunch({
				// 	url: '/pages/login/login'
				// })
				return
			}
			// console.log('角焊缝')
			this.getUserInfo()
		},
	}
</script>
<style scoped lang="less">
	.container {
		background: #FFFFFF;
	}
</style>
